package com.cqjtu.service.serviceImpl;

import com.cqjtu.mapper.TeamsMapper;
import com.cqjtu.mapper.WorkerMapper;
import com.cqjtu.pojo.PageBean;
import com.cqjtu.pojo.Worker;
import com.cqjtu.service.WorkerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class WorkerServiceImpl implements WorkerService{

    @Autowired
    private WorkerMapper workerMapper;
    @Autowired
    private TeamsMapper teamsMapper;

    //删除工人
    @Override
    public void delete(List<Integer> workerId) {
        workerMapper.delete(workerId);
    }

    //新增工人
    @Override
    public void add(Worker worker) {
        // 获取身份证中的出生日期
        String birthdayStr = worker.getIdentityCard().substring(6, 14);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date birthday = null;
        try {
            birthday = sdf.parse(birthdayStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 计算年龄
        Calendar cal = Calendar.getInstance();
        int currentYear = cal.get(Calendar.YEAR);
        cal.setTime(birthday);
        int birthYear = cal.get(Calendar.YEAR);
        worker.setAge(currentYear - birthYear);
        workerMapper.add(worker);
    }

    //工人条件查询
    @Override
    public PageBean page(Integer page, Integer pageSize, String workerName, String teamsName) {
        //设置分页参数
        List<Integer> teamsId = teamsMapper.getByName(teamsName);
        PageHelper.startPage(page,pageSize);
        //执行查询
        List<Worker> workerList = workerMapper.list(workerName,teamsId);
        Page<Worker> p = (Page<Worker>) workerList;

        //封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }


    @Override
    public Worker find(Integer workerId) {
        return workerMapper.find(workerId);
    }

    //更新工人
    @Override
    public void updateWorker(Worker worker) {
        workerMapper.updateWorker(worker);
    }

    @Override
    public PageBean page2(Integer page, Integer pageSize, Integer teamsId) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行查询
        List<Worker> workerList = workerMapper.list2(teamsId);
        Page<Worker> p2 = (Page<Worker>) workerList;

        //封装PageBean对象
        PageBean pageBean = new PageBean(p2.getTotal(),p2.getResult());
        return pageBean;
    }

    @Override
    public PageBean page3(Integer page, Integer pageSize, String workerName, String teamsName) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行查询
        List<Worker> workerList = workerMapper.list3(workerName, teamsName);
        Page<Worker> p = (Page<Worker>) workerList;

        //封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }


}
